<template>
  <div class="box">
    <div v-if="!show" class="smallBox">
      <div class="smallLeft">
        <h5>回访记录</h5>
        <div v-for="(item, index) in list" :key="index">
          <ul class="messageBox">
            <li class="boxTime">{{ item.createTime }}</li>
            <li class="boxPerson">跟进人 : {{ item.followPerson }}</li>
            <li class="boxResult">详情 : {{ item.result }}</li>
          </ul>
        </div>
      </div>
      <a class="smallRight" @click="showBox">添加</a>
    </div>
    <div v-if="show" class="showBox">
      <div class="leftBox">
        <h5>回访记录</h5>
        <div v-for="(item, index) in list" :key="index">
          <ul class="messageBox">
            <li class="boxTime">{{ item.createTime }}</li>
            <li class="boxPerson">跟进人 : {{ item.followPerson }}</li>
            <li class="boxResult">详情 : {{ item.result }}</li>
          </ul>
        </div>
      </div>
      <div class="rightBox">
        <div class="rightBoxHint">请添加此次回访结果</div>
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="medium"
          label-width="100px"
        >
          <el-form-item label="多行文本" prop="result">
            <el-input
              v-model="form.result"
              type="textarea"
              placeholder="请输入多行文本"
              :autosize="{ minRows: 4, maxRows: 4 }"
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
          <div class="rightBoxHint">请设置下次回访任务</div>
          <!-- <div v-if="visitList.length != 0" class="rightBoxHint">
            
          </div> -->
          <div class="rightBoxHint">
            部门：{{ channelFirst == '-1' ? '直接上门' : channelFirst }}
          </div>
          <div v-if="visitList.length != 0" class="rightBoxHint">
            最后有效回访时间：{{
              visitList[0].nextVisitTime == ''
                ? createTime
                : visitList[0].nextVisitTime
            }}
            00:00:00
          </div>
          <div v-else class="rightBoxHint">
            最后有效回访时间：{{ createTime }}
          </div>
          <el-row :gutter="15">
            <el-col :span="13">
              <el-form-item label="跟踪状态" prop="followStatus">
                <el-select
                  v-model="form.followStatus"
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in statusOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.label"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="13">
              <el-form-item label="下次跟进时间" prop="nextVisitTime">
                <el-date-picker
                  v-model="form.nextVisitTime"
                  type="date"
                  placeholder="下次跟进时间"
                  size="small"
                  style="width: 100%"
                  align="right"
                  :picker-options="pickerOptions"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="13">
              <el-form-item label="跟进人" prop="followPerson">
                <div class="filingBox">
                  <el-input v-model="form.followPerson" readonly />
                  <el-button type="text" @click="handleDialogue('跟进人')"
                    >选择</el-button
                  >
                </div>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="回访状态" prop="isEffectivity">
                <el-radio-group v-model="form.isEffectivity" size="medium">
                  <el-radio
                    v-for="(item, index) in visitOptions"
                    :key="index"
                    :label="item.value"
                    :disabled="item.disabled"
                    >{{ item.label }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item size="large">
            <el-button type="small" class="pan-btn blue-btn" @click="submitForm"
              >提交</el-button
            >
            <el-button type="small" @click="resetForm">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <select-model
      :show="select"
      :title="title"
      :department-staff="marketPostList"
      @close="close"
      @confirm="confirm"
    />
  </div>
</template>

<script>
import selectModel from '@/components/yiya/showOrHide/selectModel'
import store from '@/store'
import { addVisit, listVisit } from '@/api/erp/visit'

export default {
  name: 'ReturnVisit',
  components: {
    selectModel,
  },
  props: {
    createTime: {
      type: String,
      default: '',
    },
    channelFirst: {
      type: String,
      default: '',
    },
    marketPostList: {
      type: Object,
      default() {
        return {}
      },
    },
    visitList: {
      type: Array,
      default: () => [],
    },
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            },
          },
          {
            text: '明天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() + 3600 * 1000 * 24)
              picker.$emit('pick', date)
            },
          },
          {
            text: '一周后',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            },
          },
        ],
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 24 * 60 * 60 * 1000 // 返回 所有时间 大于 当前时间
        },
      },
      select: false,
      title: '',
      show: true,
      form: {
        marketReservationId: this.$route.query.id,
        result: '',
        followStatus: '待定',
        nextVisitTime: null,
        followPerson: this.$store.getters.name,
        userId: this.$store.getters.userId,
        isEffectivity: 1,
      },
      list: [],
      rules: {
        result: [
          {
            required: true,
            message: '请输入回访记录',
            trigger: 'blur',
          },
        ],
        nextVisitTime: [
          {
            required: true,
            message: '请输入预约回访时间',
            trigger: 'blur',
          },
        ],
      },
      statusOptions: [
        {
          label: '待定',
        },
        {
          label: '预约',
        },
        {
          label: '跟进',
        },
        {
          label: '过期',
        },
      ],
      visitOptions: [
        {
          label: '有效回访',
          value: 1,
        },
        {
          label: '无效回访',
          value: 2,
        },
      ],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const params = {
        marketReservationId: this.$route.query.id || this.id,
      }
      await listVisit(params).then((response) => {
        if (response.rows.length !== 0) {
          this.list = response.rows.reverse()
        }
      })
    },
    confirm(e) {
      if (e) {
        this.form.userId = e.userId
        this.form.followPerson = e.userName
        this.select = false
      } else {
        this.select = e
      }
    },
    close(e) {
      this.select = e
    },
    handleDialogue(msg) {
      this.title = msg
      this.select = true
    },
    showBox() {
      this.show = true
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.nextVisitTime = this.parseTime(
            this.form.nextVisitTime,
            '{y}-{m}-{d}'
          )
          this.form.createBy = this.$store.getters.name
          this.form.visitTime = this.parseTime(
            new Date(),
            '{y}-{m}-{d} {h}:{i}:{s}'
          )
          addVisit(this.form).then((response) => {
            if (response.code === 200) {
              this.msgSuccess('新增成功')
              this.$refs['form'].resetFields()
              this.getList()
            } else {
              this.msgError(response.msg)
            }
          })
        }
      })
    },
    resetForm() {
      this.$refs['form'].resetFields()
      this.show = false
    },
  },
}
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  display: flex;
  padding: 4px 20px 4px 0px;
  flex-direction: column;
  .smallBox {
    width: 60%;
    display: flex;
    justify-content: space-between;
    border-radius: 3px;
    border: 1px solid #dddddd;
    align-items: center;
    padding: 10px;
    margin-bottom: 20px;
    .smallLeft {
      display: flex;
      flex-direction: column;
      .messageBox {
        display: flex;
        flex-wrap: nowrap;
        list-style: none;
        .boxTime {
          text-align: right;
          font-size: 14px;
          color: #606266;
          box-sizing: border-box;
          font-weight: 700;
          line-height: 20px;
          white-space: nowrap;
        }
        .boxPerson {
          padding: 0 20px;
          white-space: nowrap;
        }
        .boxResult {
          display: block;
          float: left;
          width: 100%;
          margin-right: 10px;
          white-space: normal;
          word-break: break-all;
          word-wrap: break-word;
        }
      }
      h5 {
        padding-left: 12px;
        font-weight: 600;
        font-size: 14px;
      }
      div {
        padding-left: 12px;
        margin-bottom: 4px;
        display: flex;
        width: 100%;
        flex-wrap: nowrap;
      }
    }
    .smallRight {
      color: rgb(44, 154, 233);
    }
  }
  .showBox {
    width: 100%;
    display: flex;
    justify-content: space-between;
    .leftBox {
      display: flex;
      flex-direction: column;
      width: 49.5%;
      min-height: 500px;
      border-radius: 3px;
      border: 1px solid #dddddd;
      .messageBox {
        display: flex;
        flex-wrap: nowrap;
        list-style: none;
        .boxTime {
          text-align: right;
          font-size: 14px;
          color: #606266;
          box-sizing: border-box;
          font-weight: 700;
          line-height: 20px;
        }
        .boxPerson {
          padding: 0 20px;
        }
        .boxResult {
          display: block;
          float: left;
          width: 100%;
          margin-right: 10px;
          white-space: normal;
          word-break: break-all;
          word-wrap: break-word;
        }
      }
      h5 {
        padding-left: 12px;
        font-weight: 600;
        font-size: 14px;
      }
      div {
        padding-left: 12px;
        display: flex;
        width: 98%;
        flex-wrap: nowrap;
        margin-bottom: 4px;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
      }
      .results {
        padding-left: 0px !important;
      }
      .rightBoxHint {
        margin: 12px 0 8px 22px;
        font-weight: 600;
        font-size: 14px;
      }
    }
    .rightBox {
      width: 49.5%;
      padding: 7px 15px;
      box-shadow: 0 0 5px #666666;
      min-height: 500px;
      .rightBoxHint {
        margin: 8px 0 8px 32px;
      }
    }
  }
  .filingBox {
    display: flex;
  }
  .filingBox:nth-child(1) div {
    margin-right: 10px;
  }
}
.tagsResult {
  overflow: hidden !important;
  text-overflow: ellipsis !important;
  white-space: nowrap !important;
}
</style>
